#!/bin/sh
#
# Copyright (C) 2023  Etersoft
# Copyright (C) 2023  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

EHOG='\(apt-get\|rpm\)'
JCHAN='-t apt-get -t rpm'

__alt_epm_history_journal()
{
    a= journalctl $JCHAN
}

__alt_epm_history_uniq()
{
    __alt_epm_history_journal | grep "$EHOG\[[0-9][0-9]*\]:" | sed -e "s@.*$EHOG\[\([0-9][0-9]*\)\]: .*@\2@" | uniq | tac
}

# args: pid, removed|installed
__alt_epm_history_select()
{
    local pid="$1"
    local verb="$2"
    __alt_epm_history_journal | grep "$EHOG\[$pid\]: .*$verb" | sed -e "s@.*$EHOG\[[0-9][0-9]*\]: @@" | cut -d" " -f 1
}

_alt_epm_history_date()
{
    local pid="$1"
    __alt_epm_history_journal | grep "$EHOG\[$pid\]: " | head -n1 | cut -d" " -f 1-3,5 | sed -e 's|:$||'
}

_alt_epm_history_print_group()
{
    local i

    if [ -n "$2" ] ; then
        echo
        echo "$1 session:"
        shift
    else
        return
    fi

    for i in $* ; do
        echo "    $i"
    done
}


__alt_epm_history_removed()
{
    echo "Removed packages history:"
    __alt_epm_history_uniq | while read pid ; do
        date="$(_alt_epm_history_date $pid)"
        removed="$(epm print shortname for $(__alt_epm_history_select $pid "removed") )"
        installed="$(epm print shortname for $(__alt_epm_history_select $pid "installed") )"
        _alt_epm_history_print_group "$date" $(estrlist exclude "$installed" "$removed")
    done
}

__alt_epm_history_installed()
{
    echo "Installed packages history:"
    __alt_epm_history_uniq | while read pid ; do
        date="$(_alt_epm_history_date $pid)"
        #epm print shortname for $(__alt_epm_history_select $pid "installed") | sed -e "s|^|    |"
        removed="$(epm print shortname for $(__alt_epm_history_select $pid "removed") )"
        installed="$(epm print shortname for $(__alt_epm_history_select $pid "installed") )"
        _alt_epm_history_print_group "$date" $(estrlist exclude "$removed" "$installed")
    done
}

__alt_epm_history_updated()
{
    echo "Updated packages history:"
    __alt_epm_history_uniq | while read pid ; do
        date="$(_alt_epm_history_date $pid)"
        #epm print shortname for $(__alt_epm_history_select $pid "installed") | sed -e "s|^|    |"
        removed="$(epm print shortname for $(__alt_epm_history_select $pid "removed") )"
        installed="$(epm print shortname for $(__alt_epm_history_select $pid "installed") )"
        _alt_epm_history_print_group "$date" $(estrlist intersection "$removed" "$installed")
    done
}

epm_history_help()
{
    echo "package management history"
            get_help HELPCMD $SHAREDIR/epm-history
    cat <<EOF
Examples:
  epm history
  epm history --removed
EOF
}


epm_history()
{

if [ $PMTYPE = "apt-rpm" ] ; then
    case "$1" in
        "-h"|"--help"|"help")      # HELPCMD: help
            epm_history_help
            return
            ;;
        --installed)               # HELPCMD: print only new installed packages
            __alt_epm_history_installed
            return
            ;;
        --removed)                 # HELPCMD: print only removed packages
            __alt_epm_history_removed
            return
            ;;
        --updated)                 # HELPCMD: print only updated packages
            __alt_epm_history_updated
            return
            ;;
        --list)                    # HELPCMD: (or empty) print all history entries
            docmd journalctl $JCHAN
            return
            ;;
        "")
            ;;
        *)
            fatal "Unknown option $1. Use epm history --help to get help."
    esac
fi

[ -z "$*" ] || fatal "No arguments are allowed here"

case $PMTYPE in
    apt-rpm)
        docmd journalctl $JCHAN -r
        ;;
    apt-dpkg)
        docmd cat /var/log/dpkg.log
        ;;
    dnf-rpm)
        sudocmd dnf history
        ;;
    eopkg)
        sudocmd eopkg history
        ;;
    zypper-rpm)
        docmd cat /var/log/zypp/history
        ;;
    pacman)
        docmd cat /var/log/pacman.log
        ;;
    emerge)
        docmd cat /var/log/portage
        ;;
    *)
        fatal "Have no suitable command for $PMTYPE"
        ;;
esac

}
